<!--
 * @Author: 李天惊 uf_lee@163.com
 * @Date: 2025-02-10 11:35:51
 * @LastEditors: 李天惊 uf_lee@163.com
 * @LastEditTime: 2025-03-24 14:12:33
 * @FilePath: \global-brain\global-brain-admin\src\components\UserInfo\src\UserInfo.vue
 * Copyright (c) 2025 by ${git_name_email}, All Rights Reserved. 
-->
<script setup lang="ts">
import { ElDropdown, ElDropdownMenu, ElDropdownItem, ElMessageBox } from 'element-plus'
import { useI18n } from '@/hooks/web/useI18n'
import { useCache } from '@/hooks/web/useCache'
import { resetRouter } from '@/router'
// import { useRouter } from 'vue-router'
import { loginOutApi } from '@/api/login'
import { sysDictApi } from '@/api/dict'
import { useDesign } from '@/hooks/web/useDesign'
import { useTagsViewStore } from '@/store/modules/tagsView'

const tagsViewStore = useTagsViewStore()

const { getPrefixCls } = useDesign()

const prefixCls = getPrefixCls('user-info')

const { t } = useI18n()

const { wsCache } = useCache()
// const token = wsCache.get('token')

// const { replace } = useRouter()

const loginOut = () => {
  ElMessageBox.confirm(t('common.loginOutMessage'), t('common.reminder'), {
    confirmButtonText: t('common.ok'),
    cancelButtonText: t('common.cancel'),
    type: 'warning'
  })
    .then(async () => {
      const res = await loginOutApi().catch(() => {})
      if (res) {
        wsCache.clear()
        tagsViewStore.delAllViews()
        resetRouter() // 重置静态路由表
        window.location.href = import.meta.env.VITE_SYS_URL
      }
    })
    .catch(() => {})
}

const getDict = async () => {
  const res = await sysDictApi({}).catch(() => {})
  if (res) {
    // 字典转化
    let newDict = grouping(res.data)
    wsCache.set('dict', newDict)
  }
}

const grouping = (data: any) => {
  let map: any = {} // 处理过后的数据对象
  let temps: any = [] // 临时变量
  for (let key in data) {
    // console.log(data[key].name,data[key].name.charCodeAt(0).toString(16))
    let ekey = data[key].dict_type // 根据dict_type分组
    temps = map[ekey] || [] // 如果map里面有这个key了，就取，没有就是空数组
    temps.push(data[key])
    map[ekey] = temps
  }
  return map
}

const userInfo: any = wsCache.get('userInfo')

// const onUrl = (url) => {
//   let openUrl = url + '?token=' + token
//   window.open(openUrl, '_blank')
// }
</script>

<template>
  <ElDropdown :class="prefixCls" trigger="click">
    <div class="flex items-center">
      <img :src="userInfo.avatar" alt="" class="w-[calc(var(--logo-height)-25px)] rounded-[50%]" />
      <span class="<lg:hidden text-14px pl-[5px] text-[var(--top-header-text-color)]">
        {{ userInfo.nickname }}
      </span>
    </div>
    <template #dropdown>
      <ElDropdownMenu>
        <ElDropdownItem>
          <div @click="getDict">{{ t('更新字典') }}</div>
        </ElDropdownItem>
        <!-- <ElDropdownItem>
          <div @click="onUrl(config.url.uc_login_sso)">返回用户中心</div>
        </ElDropdownItem> -->
        <ElDropdownItem divided>
          <div @click="loginOut">{{ t('common.loginOut') }}</div>
        </ElDropdownItem>
      </ElDropdownMenu>
    </template>
  </ElDropdown>
</template>
